package com.litchishell.easy;

/**
 * @author LitchiShell
 * @description: 题目58：最后一个单词长度
 * @create 2022-08-25
 */
public class Solution011 {

    public int lengthOfLastWord(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        String[] s1 = s.split(" ");
        return s1[s1.length - 1].trim().length();
    }

    // 官方题解
    public int lengthOfLastWordCopy(String s) {
        int index = s.length() - 1;
        while (s.charAt(index) == ' ') {
            index--;
        }
        int wordLength = 0;
        while (index >= 0 && s.charAt(index) != ' ') {
            wordLength++;
            index--;
        }
        return wordLength;
    }

}
